package com.sharding.jdbc.service;

import com.sharding.jdbc.dao.OrderItemMapper;
import com.sharding.jdbc.dao.OrderMapper;
import com.sharding.jdbc.dao.RoleMapper;
import com.sharding.jdbc.model.Order;
import com.sharding.jdbc.model.OrderItem;
import com.sharding.jdbc.model.Role;
import io.shardingsphere.core.keygen.DefaultKeyGenerator;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author dxy
 * @date 2018/8/14 14:39
 */
@Service(value = "demoService")
public class DemoServiceImpl implements DemoService {
	@Resource
	private OrderMapper orderMapper;

	@Resource
	private OrderItemMapper orderItemMapper;

	@Resource
	private RoleMapper roleMapper;

	@Override
	public void demo() {
		DefaultKeyGenerator defaultKeyGenerator = new DefaultKeyGenerator();
//		orderMapper.createIfNotExistsTable();
//		orderItemMapper.createIfNotExistsTable();
//		orderMapper.truncateTable();
//		orderItemMapper.truncateTable();
		roleMapper.createIfNotExistsTable();
		int userId = 1;
		List<Long> orderIds = new ArrayList<>(100000);
		System.out.println("1.Insert--------------");
		for (int i = 0; i < 10000000; i++) {
			Order order = new Order();
			order.setUserId(userId);
			order.setStatus("INSERT_TEST");
			orderMapper.insert(order);
			long orderId = order.getOrderId();
			orderIds.add(orderId);

			OrderItem item = new OrderItem();
			item.setOrderId(orderId);
			item.setUserId(userId);
			orderItemMapper.insert(item);

//			Number number = defaultKeyGenerator.generateKey();
//			long roId = number.longValue();
			Role role = new Role();
//			role.setRoleId(roId);
			role.setAddTime(System.currentTimeMillis());
			role.setRoleName("角色名称" + i);
			role.setRoleDesc("角色描述" + i);
			role.setStatus(0);
			roleMapper.insertRole(role);

			userId++;
		}
//		System.out.println(orderItemMapper.selectAll());
//		System.out.println("2.Delete--------------");
//        for (Long each : orderIds) {
//            orderRepository.delete(each);
//            orderItemRepository.delete(each);
//        }
//        System.out.println(orderItemRepository.selectAll());
//        orderItemRepository.dropTable();
//        orderRepository.dropTable();
	}
}
